package com.event;

import cn.hutool.core.util.NetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;


/**
 * Created by mengdexuan on 2017/5/18 17:59.
 */
@Component
public class DealWithStartedEvent implements ApplicationListener<ContextRefreshedEvent> {

	private static Logger log = LoggerFactory.getLogger(DealWithStartedEvent.class);


	@Override
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		ApplicationContext appContext = contextRefreshedEvent.getApplicationContext();

		ServerProperties serverProperties = appContext.getBean(ServerProperties.class);

		String ip = NetUtil.getLocalhostStr();


		StringBuffer sb = new StringBuffer();
		sb.append("http://").append(ip).append(":").append(serverProperties.getPort());
		if (serverProperties.getContextPath()!=null)sb.append(serverProperties.getContextPath());

		log.info("项目启动完毕，访问地址 --> "+sb.toString());
	}






}
